package com.privasphere.wikidoclet.doclet;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.privasphere.wikidoclet.doclet.page.ClassPage;
import com.privasphere.wikidoclet.doclet.page.IndexPage;
import com.privasphere.wikidoclet.doclet.page.PackagePage;
import com.privasphere.wikidoclet.doclet.page.Page;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.PackageDoc;
import com.sun.javadoc.RootDoc;

/**
 * The AnalyseRootDoc Class is used to analyse a RootDoc Object from Javadoc. It
 * iterates over all Packages and Classes in the RootDoc Object and creates a
 * PackagePage for each PackageDoc Element and a ClassPage for each ClassDoc.
 * The iteration is happening Recursively
 * 
 * @author Rolf
 * @version 1.0
 */
class AnalyseRootDoc {

	// The Hashmap containing all Pages
	private List<Page> pageList;

	/**
	 * Logger Variable
	 */
	private static final Logger logger = LoggerFactory.getLogger(WikiDoclet.class);

	/**
	 * The Default Constructor creates the PageSet and stores the RootDoc Object
	 * locally
	 * 
	 * @param r
	 *            RootDoc from Javadoc
	 */
	public AnalyseRootDoc(RootDoc rootDoc) {
		this.pageList = new ArrayList<Page>();
		analyseClassDocSet(rootDoc.specifiedClasses());
		analysePackageDocSet(rootDoc.specifiedPackages());

		IndexPage indexPage = new IndexPage();
		logger.debug("Created Index Page " + indexPage.getPageTitle());
		this.pageList.add(indexPage);
	}

	/**
	 * Analyses an Array of PackageDoc Elements. For each PackageDoc Element a
	 * PackagePage is created and added to the PageSet HashMap. If a Class is
	 * found it will be analysed by AnalyseClassDocSet @see #AnalyseClassDocSet
	 * 
	 * @param pkg
	 *            PackageDoc Array
	 */
	public void analysePackageDocSet(PackageDoc[] pkg) {
		for (int i = 0; i < pkg.length; i++) {
			Page pkgpage = new PackagePage(pkg[i]);
			logger.debug("Created Packages Page " + pkgpage.getPageTitle());
			pageList.add(pkgpage);
			if (pkg[i].allClasses().length > 0) {
				analyseClassDocSet(pkg[i].allClasses());
			}
		}
	}

	/**
	 * Analyses an Array of ClassDoc Elements. For each ClassDoc Element a
	 * ClassPage is created and added to the PageSet HashMap. If a Nested Class
	 * is found it will be analysed again by AnalyseClassDocSet
	 * 
	 * @param cs
	 *            ClassDoc Array
	 */
	public void analyseClassDocSet(ClassDoc[] cs) {
		for (int i = 0; i < cs.length; i++) {
			ClassPage classpage = new ClassPage(cs[i]);
			logger.debug("Created Class Page " + classpage.getPageTitle());
			pageList.add(classpage);
//			if (cs[i].innerClasses().length > 0) {
//				//logger.debug("Found Inner Classes");
//				for(int j=0; j<cs[i].innerClasses().length; j++) {
//					//logger.debug("checking " + cs[i].innerClasses()[j].toString());
//					if(cs[i].innerClasses()[j].isPrivate()) {
//						ClassDoc[] innerclass = new ClassDoc[1];
//						innerclass[0] = cs[i].innerClasses()[j];
//						//logger.debug("Found Private Inner Classes");
//						analyseClassDocSet(innerclass);
//					}
//				}
//			}
		}
	}

	/**
	 * Getter for the PageSet Hashmap
	 * 
	 * @return PageSet Hashmap
	 */
	public List<Page> getPageSet() {
		return pageList;
	}

}
